﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.Koc;
using System.Collections;
using CSharp.Libs.Net;
using CSharp.Libs.Koc.Models;

namespace TestKocKernel2
{
    class Program
    {
        private static Kernel _KRNL = new Kernel();

        private static string _strMainMenuTitle = "Main Menu";

        private static bool _fSeedReceived = false;




        static void Main(string[] args)
        {
            //Console.WriteLine("... Press escape, a, then control X");
            //// Call ReadKey method and store result in local variable.
            //// ... Then test the result for escape.
            //ConsoleKeyInfo info = Console.ReadKey();
            //if (info.Key == ConsoleKey.Escape)
            //{
            //    Console.WriteLine("You pressed escape!");
            //}
            //// Call ReadKey again and test for the letter a.
            //info = Console.ReadKey();
            //if (info.KeyChar == 'a')
            //{
            //    Console.WriteLine("You pressed a");
            //}
            //// Call ReadKey again and test for control-X.
            //// ... This implements a shortcut sequence.
            //info = Console.ReadKey();
            //if (info.Key == ConsoleKey.X &&
            //    info.Modifiers == ConsoleModifiers.Control)
            //{
            //    Console.WriteLine("You pressed control X");
            //}
            //Console.Read();
            
            //return;




            _KRNL.SC.SeedReceived += new SeedCollector.SeedReceivedEventHandler(SC_SeedReceived);
            PhpCommandGenerator.NewUserReceived += new PhpCommandGenerator.NewUserReceivedEventHandler(PhpCommandGenerator_NewUserReceived);



            //first off, prepare Console
            _prepareConsole();

            //at last, show Menu
            _mainMenu();


            PhpCommandGenerator.NewUserReceived -= PhpCommandGenerator_NewUserReceived;
            _KRNL.SC.SeedReceived -= SC_SeedReceived;
            _KRNL = null;
        }



        static void _prepareConsole()
        {
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.Clear();
            
            //Console.SetWindowSize(160, 79); //Maximum
            //Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);//Maximum
            Console.SetWindowSize(120, 60);


            
        }



        #region main menu functions
        static void _mainMenu()
        {
            Console.Clear();

            int intStartingRow = 1;
            int intMainMenuPosition = _calcCenterPosition(_strMainMenuTitle, Console.WindowWidth);
            Console.SetCursorPosition(intMainMenuPosition, intStartingRow++);
            Console.WriteLine(_strMainMenuTitle);
            intStartingRow++;

            Console.SetCursorPosition(intMainMenuPosition - 5, intStartingRow++);
            Console.WriteLine("'1' Show Seeds List");

            Console.SetCursorPosition(intMainMenuPosition - 5, intStartingRow++);
            Console.WriteLine("'2' Submit first Seeds URL");

            Console.SetCursorPosition(intMainMenuPosition - 5, intStartingRow++);
            Console.WriteLine("'T' Manual Submit Test");

            Console.SetCursorPosition(intMainMenuPosition - 5, intStartingRow++);
            Console.WriteLine("'R' Refresh Menu");

            Console.SetCursorPosition(intMainMenuPosition - 5, intStartingRow++);
            Console.WriteLine("'Q' Quit");

            Console.SetCursorPosition(0, 10);
            for (int i = 0; i < Console.WindowWidth; i++)
            {
                Console.Write("=");
            }

            //reset position
            _setCursorStandardPosition();
            
            
            //set Infos
            _setSeedReceivedInfo();
            _setUserListInfo();



            //await keys
            bool done = false;
            while (!done)
            {
                ConsoleKeyInfo cki = Console.ReadKey(true);
                switch (cki.Key)
                {
                    case ConsoleKey.Q:
                        return;

                    case ConsoleKey.D1:
                        _showSeedsList();
                        break;

                    case ConsoleKey.D2:
                        PostSubmitter PS = new PostSubmitter();
                        Hashtable htSeed=_KRNL.SC.Seeds.First().Value as Hashtable;
                        PS.Url = htSeed["FullUrl"].ToString();
                        PS.Post();
                        PS = null;
                        break;

                    case ConsoleKey.T:
                        _manualTestSubmit();
                        break;

                    case ConsoleKey.R:
                        //_prepareConsole();
                        _mainMenu();
                        break;
                }
                Console.Read();
            }
        }

        static int _calcCenterPosition(
            string strText,
            int intMaxLeft)
        {
            return (intMaxLeft / 2) - (strText.Length - 2);
        }

        static void _setCursorStandardPosition()
        {
            Console.SetCursorPosition(0, 11);
        }
        #endregion



        #region seed received info
        static void _setSeedReceivedInfo()
        {
            ConsoleExtensions.RememberCursorLocation();

            Console.SetCursorPosition(0, 9);
            Console.Write("Seeds received: " + (_fSeedReceived ? "Yes" : "No") + "   Seed count: " + _KRNL.SC.Seeds.Count);

            ConsoleExtensions.ResetCursorLocation();
        }

        static void SC_SeedReceived(object sender, CSharp.Libs.Koc.KernelModels.SeedReceivedEventArgs srea)
        {
            _fSeedReceived = true;
            _setSeedReceivedInfo();

            //Console.WriteLine(srea.SeedText);
        }
        #endregion



        #region user data received info
        static void _setUserListInfo()
        {
            //          1         2         3
            //0123456789012345678901234567890123456789
            //       User List
            //FB-UID           Valid
            //123456789012     True

            int intOffsetFBUID = Console.WindowWidth - 23;
            int intOffsetValid = intOffsetFBUID + 17;
            int intRow = 1;
            
            Console.SetCursorPosition(intOffsetFBUID, intRow++);
            Console.Write("       User List");
            Console.SetCursorPosition(intOffsetFBUID, intRow++);
            Console.Write("FB-UID");
            Console.SetCursorPosition(intOffsetValid, intRow - 1);
            Console.Write("Valid");
            Console.SetCursorPosition(intOffsetFBUID, intRow++);
            Console.Write("----------------------");
            
            foreach (KeyValuePair<string, UserValidatorItem> kvp in PhpCommandGenerator.Users)
            {
                Console.SetCursorPosition(intOffsetFBUID, intRow++);
                Console.Write(kvp.Key);
                Console.SetCursorPosition(intOffsetValid, intRow - 1);
                Console.Write(kvp.Value.UserIsValid.ToString());
            }


            _setCursorStandardPosition();
        }

        static void PhpCommandGenerator_NewUserReceived(object sender, CSharp.Libs.Koc.KernelModels.NewUserReceivedEventArgs nuvea)
        {
            _setUserListInfo();
        }
        #endregion

        
        
        #region functions
        static void _showSeedsList()
        {
            if (_fSeedReceived)
            {
                ConsoleExtensions.RememberCursorLocation();

                _setCursorStandardPosition();

                foreach (KeyValuePair<string, object> kvp in _KRNL.SC.Seeds)
                {
                    Hashtable htSeed = kvp.Value as Hashtable;
                    Console.WriteLine("Key: '" + kvp.Key + "'     Hashentries: " + htSeed.Count);
                    Console.WriteLine("     FullUrl: '" + htSeed["FullUrl"]);
                }

                ConsoleExtensions.ResetCursorLocation();
            }
        }



        static void _manualTestSubmit()
        {
            PostSubmitter PS = new PostSubmitter();
            PS.Url = "http://apps.facebook.com/kingdomsofcamelot/?lang=en&s=379";
            string strResponse = PS.Post();
            PS = null;
        }
        #endregion

    }



    static class ConsoleExtensions
    {
        private static int _intLeft = 0, _intTop = 0;

        public static void RememberCursorLocation()
        {
            _intLeft = Console.CursorLeft;
            _intTop = Console.CursorTop;
        }

        public static void ResetCursorLocation()
        {
            Console.SetCursorPosition(_intLeft, _intTop);
        }
    }
}
 